package controller;

import domain.CorderResult;
import domain.order;
import execption.lackGoodException;
import execption.nullValueException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import service.CustomerService;
import service.impl.CustomerServiceImpl;
import  utill.*;
import utill.component.promptBox;

import java.util.List;

public class CustomerOrderController {

    @FXML
    private HBox BuyTitleFlowerHbox;

    @FXML
    private TableColumn<?, ?> viewflowerId;

    @FXML
    private TableColumn<?, ?> viewOrderDate;

    @FXML
    private TableColumn<?, ?> ViewStroneId;

    @FXML
    private Label HLabelType;

    @FXML
    private MenuItem signout;

    @FXML
    private Label HLabelBuyNumber;

    @FXML
    private TextField HTextBuyNumber;

    @FXML
    private HBox TitleBox;

    @FXML
    private TextField HTextStoreNumber;

    @FXML
    private Menu TitleBuyFlower;

    @FXML
    private TextField HTextFlowerType;

    @FXML
    private Button btnClerar;

    @FXML
    private TableView<CorderResult> TableViewShowOrder;

    @FXML
    private Label BuyFlowerTitle;

    @FXML
    private Menu btnSignout;

    @FXML
    private AnchorPane CumstomerShow;

    @FXML
    private Label HLabelStoreNumber;

    @FXML
    private MenuBar TitleMenuBar;

    @FXML
    private Menu btnshowOrder;

    @FXML
    private MenuItem btnBuyFlower;

    @FXML
    private MenuItem help;

    @FXML
    private Menu btnSelect;

    @FXML
    private MenuItem Select;

    @FXML
    private Button btncommit;

    @FXML
    private Menu btnHelp;

    @FXML
    private MenuItem showOrder;

    @FXML
    private TableColumn<?, ?> viewFlowerPrice;

    @FXML
    private TableColumn<?, ?> viewOrderPirce;


    String SignOutUrl="/controller/login.fxml";
    String backUrl="/controller/customerSelect.fxml";

    CustomerService customerService =new CustomerServiceImpl();
    @FXML
    void clickSignout(ActionEvent event) throws Exception {
     ParamUtil.userId.remove(Thread.currentThread()+"userId");
     switchpages switchpage =new switchpages();
     switchpage.switch_viewPane(this.CumstomerShow,SignOutUrl);
    }


    @FXML
    void clear(ActionEvent event) {
        clearContext();
    }

    @FXML
    void commit(ActionEvent event) {
        String type =HTextFlowerType.getText();
        String number=HTextBuyNumber.getText();
        String storeNumber=HTextStoreNumber.getText();
        if("".equals(type) || "".equals(number) || "".equals(storeNumber)){
            promptBox.tipsMessage("请输入完整信息");
        }
        if(!"".equals(type) && !"".equals(number) || !"".equals(storeNumber)){
            order o =new order();
            o.setFlowertype(type);
            o.setFlowernumber(Integer.valueOf(number));
            o.setStoreId(Integer.valueOf(storeNumber));
            boolean flag=false;
            try {
                flag=customerService.AddOrder(o);
            } catch (lackGoodException e) {
                promptBox.tipsMessage(e.getMessage());
                e.printStackTrace();
            } catch (nullValueException e) {
                promptBox.tipsMessage(e.getMessage());
                e.printStackTrace();
            }finally {
                if(flag){
                    promptBox.tipsMessage("购买成功");
                }else{
                    promptBox.tipsMessage("购买失败");
                }
            }


            showData();
        }
    }


    @FXML
    void buyFlowers(ActionEvent event){
        String type =HTextFlowerType.getText();
        String number=HTextBuyNumber.getText();
        String storeNumber=HLabelStoreNumber.getText();
        if("".equals(type) || "".equals(number) || "".equals(storeNumber)){
            promptBox.tipsMessage("请输入完整信息");
        }
        if(!"".equals(type) && !"".equals(number) || !"".equals(storeNumber)){
            order o =new order();
            o.setFlowertype(type);
            o.setFlowernumber(Integer.valueOf(number));
            o.setStoreId(Integer.valueOf(storeNumber));
            boolean flag=false;
            try {
              flag=  customerService.AddOrder(o);
            } catch (lackGoodException e) {
                promptBox.tipsMessage(e.getMessage());
                e.printStackTrace();
            } catch (nullValueException e) {
                promptBox.tipsMessage(e.getMessage());
                e.printStackTrace();
            }finally {
                if(flag){
                    promptBox.tipsMessage("购买成功");
                }else{
                    promptBox.tipsMessage("购买失败");
                }
            }
            showData();
        }
    }

    @FXML
    void checkOrder(ActionEvent event){
        showData();
    }

    @FXML
    void  callback(ActionEvent event) throws Exception {
          switchpages switchpage=new switchpages();
          switchpage.switch_viewPane(this.CumstomerShow,backUrl);
    }


    void clearContext(){
        HTextFlowerType.setText("");
        HTextBuyNumber.setText("");
        HTextStoreNumber.setText("");
    }


    public void bindTableView(TableView  tableView ,  TableColumn column1 ,
                              TableColumn column2 ,TableColumn column3,
                              TableColumn column4 ,TableColumn column5){
        column1.setCellValueFactory(new PropertyValueFactory<>("storeName"));
        column2.setCellValueFactory(new PropertyValueFactory<>("flowerType"));
        column3.setCellValueFactory(new PropertyValueFactory<>("flowerPrice"));
        column4.setCellValueFactory(new PropertyValueFactory<>("flowertotal"));
        column5.setCellValueFactory(new PropertyValueFactory<>("flowerBuyTime"));
    }



    public  void showData(){

        TableViewShowOrder.getItems().clear();
        bindTableView(TableViewShowOrder,ViewStroneId,viewflowerId,viewFlowerPrice,viewOrderPirce,viewOrderDate);
        List<CorderResult> corderResultList=customerService.findOrders();
        ObservableList<CorderResult> data = FXCollections.observableArrayList();

        for(CorderResult c : corderResultList){

              data.add(c);
        }
        TableViewShowOrder.setItems(data);
    }




}
